package springboot.Controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

import springboot.Controller.dto.CollectorDTO;
import springboot.Controller.dto.UserDTO;
import springboot.common.Constants;
import springboot.common.Result;
import springboot.entity.LjhsUser;
import springboot.service.ILjhsCollectorService;
import springboot.entity.LjhsCollector;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hys
 * @since 2022-06-02
 */
@RestController
@RequestMapping("/collector")
public class LjhsCollectorController {
    @Resource
    private ILjhsCollectorService ljhsCollectorService;

    @PostMapping("/login")
    public Result Login(@RequestBody CollectorDTO collectorDTO, HttpSession session){
        String collector = collectorDTO.getCollector();
        String password = collectorDTO.getPassword();
        if (StrUtil.isBlank(collector)||StrUtil.isBlank(password)) {
            return Result.error(Constants.CODE_400,"参数错误");
        }
        CollectorDTO dto = ljhsCollectorService.login(collectorDTO);
        return Result.success(dto);
    }
    @PostMapping("/register")

    public Result Register(@RequestBody CollectorDTO collectorDTO){
        String collector = collectorDTO.getCollector();
        String password = collectorDTO.getPassword();
        collectorDTO.setCommission(BigDecimal.valueOf(0.00));
        if (StrUtil.isBlank(collector)||StrUtil.isBlank(password)) {
            return Result.error(Constants.CODE_400,"参数错误");
        }

        return Result.success(ljhsCollectorService.register(collectorDTO));

    }
    @PostMapping

    public Result save(@RequestBody LjhsCollector ljhsCollector){

        return Result.success(ljhsCollectorService.saveOrUpdate(ljhsCollector));
        }

        @DeleteMapping("/{id}")

        public Result delete(@PathVariable Integer id){

        return Result.success(ljhsCollectorService.removeById(id));
        }
        @PostMapping("/batch/del")
        public Result deleteBatch(@RequestBody List<Integer> ids){

        return Result.success(ljhsCollectorService.removeByIds(ids));
        }
        @GetMapping

        public Result index(){

        return Result.success(ljhsCollectorService.list());
        }

        @GetMapping("/{id}")

        public Result findOne(@PathVariable Integer id) {
        return Result.success(ljhsCollectorService.getById(id));
        }

        @GetMapping("/page")

        public Result findPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String collector) {
            IPage<LjhsCollector> page = new Page<>(pageNum, pageSize);
            QueryWrapper<LjhsCollector> queryWrapper = new QueryWrapper<>();
            if (!"".equals(collector)) {
                queryWrapper.like("collector",collector);
            }
            queryWrapper.orderByDesc("collectorId");
            return Result.success(ljhsCollectorService.page(page,queryWrapper));
        }


}

